package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import concole.Administrator;
import concole.IOHelper;
import concole.Identification;
import concole.Student;
import concole.Teacher;
import concole.User;

public class LoginEntry extends JFrame {
	protected final int TEACHER = 1;
	protected final int STUDENT = 2;
	protected final int ADMIN = 0;
	protected JFrame loginFrame;
	protected JTextField idField;
	protected JTextField checkCodeField;
	protected JPasswordField passwordField;
	protected JRadioButton teacherRadioButton;
	protected JRadioButton studentRadioButton;
	protected JRadioButton adminRadioButton;
	protected CheckCodePanel codePanel;
	protected int identification = TEACHER;
	protected String checkCode;
	protected User user;

	public LoginEntry() {

		loginFrame = new JFrame();
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int frameWidth = 0;
		int frameHeight = 0;
		if (screenWidth * 3 > screenHeight * 4) {
			frameHeight = screenHeight / 2;
			frameWidth = frameHeight * 4 / 3;
		} else {
			frameWidth = screenWidth / 2;
			frameHeight = frameWidth * 3 / 4;
		}
		loginFrame.setBounds((screenWidth - frameWidth) / 2,
				(screenHeight - frameHeight) / 2, frameWidth, frameHeight);
		loginFrame.setTitle("Login");
		loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		loginFrame.setIconImage(new ImageIcon("Icon/login.jpg").getImage());

		MyPanel myPanel = new MyPanel();
		loginFrame.getContentPane().add(myPanel);
		/** buttons */
		JButton loginButton = new JButton("Login");
		loginButton.setLocation(frameWidth * 2 / 5, 0);
		loginButton.setSize(frameWidth / 5, frameHeight / 10);
		loginButton.addActionListener(new LoginListener());
		JButton exitButton = new JButton("Exit");
		loginButton.setLocation(frameWidth * 2 / 5, 0);
		exitButton.setSize(frameWidth / 5, frameHeight / 10);
		exitButton.addActionListener(new ExitListener());

		JButton registerButton = new JButton("Registe");
		loginButton.setLocation(frameWidth * 2 / 5, 0);
		registerButton.setSize(frameWidth / 5, frameHeight / 10);
		registerButton.addActionListener(new RegisterListener());

		MyPanel buttonsPanel = new MyPanel();
		buttonsPanel.setSize(frameWidth, frameHeight / 10);
		buttonsPanel.setLocation(0, frameHeight * 6 / 9);
		buttonsPanel.setLayout(new FlowLayout());
		buttonsPanel.add(loginButton);
		buttonsPanel.add(registerButton);
		buttonsPanel.add(exitButton);
		/*About the checkcode panel*/
		MyPanel checkPanel=new MyPanel();
		checkPanel.setLayout(null);
		checkPanel.setSize(frameWidth, frameHeight / 10);
		checkPanel.setLocation(0, frameHeight * 5 / 9);
		codePanel=new CheckCodePanel();
		codePanel.setBounds(frameWidth * 16 / 30, 6,frameWidth * 9 / 20, frameHeight / 10);
		checkPanel.add(codePanel);
		JLabel codeLabel=new JLabel("Checkcode:");
		codeLabel.setBounds(frameWidth / 6, 0,frameWidth / 5, frameHeight / 10);
		checkPanel.add(codeLabel);
		checkCodeField=new JTextField();
		checkCodeField.setBounds(frameWidth * 6/ 20, 0,frameWidth * 4 / 20, frameHeight / 10);
		checkPanel.add(checkCodeField);
		/** about the id Panel */
		MyPanel idJPanel = new MyPanel();
		idJPanel.setLayout(null);
		idJPanel.setSize(frameWidth, frameHeight / 10);
		idJPanel.setLocation(0, frameHeight * 3 / 9);
		JLabel idLabel = new JLabel("Your ID:");
		idLabel.setSize(frameWidth / 5, frameHeight / 10);
		idLabel.setLocation(frameWidth / 6, 0);
		idField = new JTextField("");
		idField.setSize(frameWidth * 9 / 20, frameHeight / 10);
		idField.setLocation(frameWidth * 3 / 10, 0);
		idJPanel.add(idLabel);
		idJPanel.add(idField);

		/** the password Panel */
		MyPanel passwordPanel = new MyPanel();
		passwordPanel.setLayout(null);
		passwordPanel.setSize(frameWidth, frameHeight / 10);
		passwordPanel.setLocation(0, frameHeight * 4 / 9);
		JLabel passwordLabel = new JLabel("Password:");
		passwordLabel.setSize(frameWidth / 5, frameHeight / 10);
		passwordLabel.setLocation(frameWidth / 6, 0);
		passwordField = new JPasswordField();
		passwordField.setSize(frameWidth * 9 / 20, frameHeight / 10);
		passwordField.setLocation(frameWidth * 3 / 10, 0);
		passwordPanel.add(passwordLabel);
		passwordPanel.add(passwordField);

		MyPanel choosePanel = new MyPanel();
		choosePanel.setLayout(null);
		choosePanel.setSize(frameWidth, frameHeight / 10);
		choosePanel.setLocation(0, frameHeight * 2 / 9);
		JLabel roleLabel = new JLabel("User role:");
		roleLabel.setSize(frameWidth / 5, frameHeight / 10);
		roleLabel.setLocation(frameWidth / 6, 0);
		adminRadioButton = new JRadioButton("admin", false);
		adminRadioButton.setSize(frameWidth / 7, frameHeight / 10);
		adminRadioButton.setLocation(frameWidth * 3 / 10, 0);
		adminRadioButton.setBackground(Color.white);
		adminRadioButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				adminRadioButton.setForeground(Color.red);
				teacherRadioButton.setForeground(Color.black);
				studentRadioButton.setForeground(Color.black);
				identification = ADMIN;
			}
		});

		teacherRadioButton = new JRadioButton("Teacher", true);
		teacherRadioButton.setForeground(Color.red);
		teacherRadioButton.setSize(frameWidth / 7, frameHeight / 10);
		teacherRadioButton.setLocation(frameWidth * 18 / 40, 0);
		teacherRadioButton.setBackground(Color.white);
		teacherRadioButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				adminRadioButton.setForeground(Color.black);
				teacherRadioButton.setForeground(Color.red);
				studentRadioButton.setForeground(Color.black);
				identification = TEACHER;
			}
		});
		studentRadioButton = new JRadioButton("Student", false);
		studentRadioButton.setSize(frameWidth / 7, frameHeight / 10);
		studentRadioButton.setLocation(frameWidth * 24 / 40, 0);
		studentRadioButton.setBackground(Color.white);
		studentRadioButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				studentRadioButton.setForeground(Color.red);
				adminRadioButton.setForeground(Color.black);
				teacherRadioButton.setForeground(Color.black);
				identification = STUDENT;
			}
		});
		ButtonGroup userButtonGroup = new ButtonGroup();
		userButtonGroup.add(adminRadioButton);
		userButtonGroup.add(teacherRadioButton);
		userButtonGroup.add(studentRadioButton);

		choosePanel.add(roleLabel);
		choosePanel.add(adminRadioButton);
		choosePanel.add(teacherRadioButton);
		choosePanel.add(studentRadioButton);


		myPanel.setLayout(null);
		myPanel.add(choosePanel);
		myPanel.add(idJPanel);
		myPanel.add(passwordPanel);
		myPanel.add(buttonsPanel);
		myPanel.add(checkPanel);
		loginFrame.setVisible(true);
	}

	class LoginListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			boolean checkCodeRight=judgeCheckCode();
			if(!checkCodeRight){
				JOptionPane.showMessageDialog(null,"Sorry your checkCode is wrong input again");
				checkCodeField.setText("");
				return;
			}
			if (studentRadioButton.isSelected()) {
				user = new Student(idField.getText(), passwordField
						.getPassword().toString().trim(), true);
				user.setIdentity(Identification.STUDENT);
			} else if (teacherRadioButton.isSelected()) {
				user = new Teacher(idField.getText(), passwordField
						.getPassword().toString().trim(), true);
				user.setIdentity(Identification.TEACHER);
			} else {
				user = new Administrator(idField.getText(), passwordField
						.getPassword().toString().trim());
				user.setIdentity(Identification.ADMINISTRATOR);
			}

			boolean valid = checkCompitabel(user);

			if (valid) {
				JOptionPane.showMessageDialog(null,
						"You hace successfully logined");
				LoginEntry.this.shutDown();

				switch (user.getIdentity()) {
				case ADMINISTRATOR:
					startAdminBorad(user.getNum(), user.getPassword(), 1);
					
					break;
				case TEACHER:
					startTeacherBoard(user.getNum(), 1);
					
					break;
				case STUDENT:
					startStudentBoard(user.getNum(), 1);
					
					break;
				}

			}
		}

	}

	protected class ExitListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			System.exit(0);

		}

	}

	class RegisterListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			LoginEntry.this.shutDown();
			startRegister();
		}

	}

	public boolean checkCompitabel(User user) {
		boolean isCompitable = false;
		char[] storePassword = null;
		System.out.println(user.getNum() + " " + user.getPassword());
		System.out.println(IOHelper.findOneRecord(0, this.getFileName(),
				user.getNum()));
		String storePasswordStr = IOHelper.findOneRecord(0, this.getFileName(),
				user.getNum());
		if (storePasswordStr != null) {
			storePassword = (storePasswordStr.split(" "))[1].toCharArray();
			if (Arrays.equals(passwordField.getPassword(), storePassword)) {
				isCompitable = true;
			} else {
				JOptionPane.showMessageDialog(null, "sorry password wrong");
				isCompitable = false;
			}
		} else {
			JOptionPane.showMessageDialog(null, "sorry account not found");
		}

		System.out.println("father");
		return isCompitable;
	}

	public void startRegister() {
		// TODO Auto-generated method stub
		new RegisterEnter();
	}
	public boolean judgeCheckCode(){
		boolean result=true;
		checkCode=codePanel.getCode();
        result=checkCode.equals(checkCodeField.getText());
        System.out.println(result);
		return result;
	}

	public void startStudentBoard(String num, int i) {
		// TODO Auto-generated method stub
		StudentBorad studentBorad = new StudentBorad(user.getNum(),
				1);
		
	}

	public void startTeacherBoard(String num, int i) {
		// TODO Auto-generated method stub
		TeacherBorad teacherBorad = new TeacherBorad(user.getNum(),
				1);

	}

	public void startAdminBorad(String num, String password, int i) {
		// TODO Auto-generated method stub
		AdminBoard adminBoard = new AdminBoard(user.getNum(),
				user.getPassword(), 1);

	}

	public boolean checkExitence(User user) {
		boolean isExistened = false;
		return isExistened;
	}

	public int getIdentification() {
		return this.identification;
	}

	public String getFileName() {
		String fileName = "";
		switch (this.identification) {
		case ADMIN:
			fileName = "LoginAccounts.txt";
			break;
		case TEACHER:
			fileName = "teacherAccounts.txt";
			break;
		case STUDENT:
			fileName = "studentAccounts.txt";
			break;
		default:
			break;
		}
		return fileName;
	}

	protected void shutDown() {
		loginFrame.setVisible(false);
	}

}

class MyPanel extends JPanel {
	public void paintComponent(Graphics c) {
		ImageIcon backGround = new ImageIcon("Icon/background.jpg");
		c.drawImage(backGround.getImage(), 0, 0, this);
	}
}
